package com.dps.omc.utils;

import com.dps.omc.task.message.DesUtils;
import com.dps.omc.task.message.PassConstants;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by lip on 2016-04-25.
 */
public class RegexpUtil {

    private final static String regxpForPass = PassConstants.PASS_START+"(.*)"+PassConstants.PASS_END;

    public static String filter(String str) throws Exception {
        Pattern pattern = Pattern.compile(regxpForPass);
        Matcher matcher = pattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        boolean result1 = matcher.find();
        while (result1) {
//            System.out.println(matcher.group(1));
            String pass = DesUtils.decrypt(matcher.group(1));
            matcher.appendReplacement(sb, pass);
            result1 = matcher.find();
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static void main(String[] args) throws Exception {
        String msg = "mysql -h 10.238.6.92 -ulp -p@d7d6cf63d5b78fafbb56fa538f9054eaaf73d7155882624a# testrms</root/1.sql";
        msg = RegexpUtil.filter(msg);

        System.out.println(msg);
    }
}
